/**
 * 地图模块 - 村庄预案
 */
export default {
    data() {
        return {
            width: 38,
            height: 60,
            legend: [
                { name: '无预案', icon: require('@/assets/map/village/YA_0.png') },
                { name: '有预案', icon: require('@/assets/map/village/YA_1.png') }
            ]
        }
    },
    // 计算属性
    computed: {
        // 流域编码
        basin() {
            return this.$store.getters.basin
        }
    },
    // 生命周期-创建
    created() {
        this.$eventBus.$on('flyToVillage_CZYA', this.flyToVillShowPopCZYA)
        this.entitiesPonitCZYA = new Map()
        // this._dataSourcePointCZYA = new Cesium.CustomDataSource('pointCZYA')
    },
    // 生命周期-销毁前
    beforeDestroy() {
    },
    // 生命周期-销毁
    destroyed() {
        // 清理
        this.clearCZYA()

        this.$store.getters.eventCesiumOption.leftPickFns.delete('CZYA')
    },
    // 生命周期-挂载
    mounted() {
    },
    methods: {
        /**
         * 模块构建
         * @param {object} params 请求参数信息
         */
        creatCZYA(params) {
            console.log('模块构建-CZYA')
            this.clearCZYA()

            var urlApi = '/api/Plan/GetPlanList'
            this.$api.Get(urlApi, {
                ...params,
                ...{
                    page: 1,
                    limt: 999999
                }
            }).then(res => {
                res.data.filter((f, i, a) => {
                    // return a.findIndex(v => { return v.addvcd === f.addvcd }) === i
                    return f.lgtd != null && f.lttd != null && a.findIndex(v => { return v.addvcd === f.addvcd }) === i
                }).forEach(item => {
                    var lv = item.wordPath != null ? 1 : 0
                    // item.lgtd == null && (item.lgtd = 109.6 + Math.random() * 0.7)
                    // item.lttd == null && (item.lttd = 34 + Math.random() * 0.7)
                    var entity = {
                        eventName: 'CZYA',
                        prop: item,
                        position: Cesium.Cartesian3.fromDegrees(
                            Number(item.lgtd),
                            Number(item.lttd)
                        ),
                        billboard: {
                            image: this.legend[lv].icon,
                            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                            heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, this.$config.mapConfig.pb_far),
                            // width: this.$config.mapConfig.billboardSize,
                            // height: this.$config.mapConfig.billboardSize
                            width: this.width,
                            height: this.height
                        }
                    }
                    this.entitiesPonitCZYA.set(item.addvcd, this.$viewer.entities.add(entity))
                    // this._dataSourcePointCZYA.entities.add(entity)
                })
                // this.$viewer.dataSources.add(this._dataSourcePointCZYA)
                this.initEvevtCZYA()
            })
        },
        /**
         * 模块事件初始化
         */
        initEvevtCZYA() {
            var eventFn = pickedObject => {
                if (
                    pickedObject &&
                    pickedObject.id &&
                    pickedObject.id.eventName
                ) {
                    if (pickedObject.id.eventName === 'CZYA') {
                        this.flyToVillShowPopCZYA(pickedObject.id.prop.addvcd)
                    }
                }
            }
            this.$store.getters.eventCesiumOption.leftPickFns.set('CZYA', eventFn)
        },
        flyToVillShowPopCZYA(adcd) {
            var entity = this.entitiesPonitCZYA.get(adcd)
            if (entity) {
                this.$viewer.flyTo(entity, {
                    duration: 0.5,
                    offset: {
                        heading: this.$viewer.camera.heading,
                        pitch: this.$viewer.camera.pitch,
                        range: 5000
                    }
                })
                const popStayParams = this.$store.getters.popStayParams
                entity.prop.eventName = 'CZYA'
                popStayParams.name = '村庄预案信息'
                // popStayParams.posX = Math.round(event.position.x)
                // popStayParams.posY = Math.round(event.position.y)
                popStayParams.remarks = entity.prop
                this.$store.dispatch('popStayParams', popStayParams)
            }
        },
        /**
         * 模块清理
         */
        clearCZYA() {
            if (this.entitiesPonitCZYA && this.entitiesPonitCZYA.size > 0) {
                this.entitiesPonitCZYA.forEach((val, key, map) => {
                    this.$viewer.entities.remove(val)
                    map.delete(key)
                })
                // this.entitiesPonitCZYA = new Map()
            }
            this.$store.dispatch('closePop')
            this.$store.dispatch('closePopStay')
            // if (this._dataSourcePointCZYA &&
            //     this._dataSourcePointCZYA.entities &&
            //     this._dataSourcePointCZYA.entities.removeAll
            // ) {
            //     this._dataSourcePointCZYA.entities.removeAll()
            // }
            if (this.screenSpaceEventHandlerCZYA) {
                this.screenSpaceEventHandlerCZYA.destroy()
                this.screenSpaceEventHandlerCZYA = null
            }
        },
        /**
         * 显示模块
         */
        showCZYA() {
            if (this.entitiesPonitCZYA && this.entitiesPonitCZYA.size > 0) {
                this.entitiesPonitCZYA.forEach((val, key, map) => {
                    val.show = !0
                })
            }
            // if (this._dataSourcePointCZYA &&
            //     this._dataSourcePointCZYA.entities &&
            //     !this._dataSourcePointCZYA.entities.show
            // ) {
            //     this._dataSourcePointCZYA.entities.show = !0
            // }
        },
        /**
         * 隐藏模块
         */
        hideCZYA() {
            if (this.entitiesPonitCZYA && this.entitiesPonitCZYA.size > 0) {
                this.entitiesPonitCZYA.forEach((val, key, map) => {
                    val.show = !1
                })
            }
            // if (this._dataSourcePointCZYA &&
            //     this._dataSourcePointCZYA.entities &&
            //     this._dataSourcePointCZYA.entities.show
            // ) {
            //     this._dataSourcePointCZYA.entities.show = !1
            // }
        }
    }
}
